﻿namespace Stopwatch.ViewModels
{
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.ComponentModel;

    public sealed class SplitItems : IEnumerable<SplitItem>, INotifyCollectionChanged
    {
        List<SplitItem> items;

        public SplitItems()
        {
            items = new List<SplitItem>();
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        public IEnumerator<SplitItem> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(SplitItem item)
        {
            item.Order = items.Count + 1;
            items.Add(item);

            if (CollectionChanged != null)
            {
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, item.Order - 1));
            }
        }

        public void Clear()
        {
            items.Clear();

            if (CollectionChanged != null)
            {
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }
    }

    public class SplitItem : INotifyPropertyChanged
    {
        public SplitItem(Time time)
        {
            splitTime = time.ToWatch();
        }

        private int order;

        public int Order
        {
            get
            {
                return order;
            }
            set
            {
                if (value != order)
                {
                    order = value;
                    NotifyPropertyChanged("Order");
                }
            }
        }

        private string splitTime;

        public string SplitTime
        {
            get
            {
                return splitTime;
            }
            set
            {
                if (value != splitTime)
                {
                    splitTime = value;
                    NotifyPropertyChanged("SplitTime");
                }
            }
        }

        private string originalTime;

        public string OriginalTime
        {
            get
            {
                return originalTime;
            }
            set
            {
                if (value != originalTime)
                {
                    originalTime = value;
                    NotifyPropertyChanged("OriginalTime");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}